package whf.framework.web.servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import whf.framework.ext.entity.MenuItem;
import whf.framework.ext.service.MenuItemServiceImp;
import whf.framework.log.util.LogUtil;
import whf.framework.util.Base64;
import whf.framework.util.ParamUtils;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;

/**
 * @author wanghaifeng
 *
 */
public class MenuDispatchServlet extends HttpServlet {
	private final static String ERROR_PAGE = "error";
	private final static String HELP_PAGE = "help";
	
	private Map<String, String> parameters = Utils.newHashMap();
	
	private boolean logUri;
	
	public String getInitParameter(String paramName){
		String value = super.getInitParameter(paramName);
		if(value != null && !value.startsWith("/")){
			value = "/"+value;
		}
		return value;
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		Enumeration paramNames = this.getInitParameterNames();
		while(paramNames.hasMoreElements()){
			String paramName = (String)paramNames.nextElement();
			this.parameters.put(paramName, this.getInitParameter(paramName));
		}
		//
		this.logUri = "true".equalsIgnoreCase(config.getInitParameter("logUri"));
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long id = ParamUtils.getLongParameter(request,"id", 0);
		String type = request.getParameter("type");
		if(StringUtils.equalsIgnoreCase("menu", type)){
			MenuItem menu = null;
			try{
				menu = MenuItemServiceImp.getMenuItemService().findByPrimaryKey(id);
			}catch(Exception e){
				//
			}
			if(menu == null){
				response.sendRedirect(request.getContextPath()+this.parameters.get(ERROR_PAGE));
			} else {
				String url = menu.getHref().trim();
				if(!url.startsWith("/")){
					url = "/"+url;
				}
				if(this.logUri) {
					LogUtil.log(request, "menu", "menu", url, 0);
				}
				response.sendRedirect(request.getContextPath()+url);
			}
		} else if(StringUtils.equalsIgnoreCase("help", type)){
			request.getRequestDispatcher(this.parameters.get(HELP_PAGE)).forward(request, response);
		} else if(StringUtils.equalsIgnoreCase("url", type)){
			String url = request.getParameter("url");
			url = new String(Base64.decode(url));
			if(!url.startsWith("/")){
				url = "/"+url;
			}
			request.getRequestDispatcher(url).forward(request, response);
		} else if(this.parameters.keySet().contains(type)){
			String value = this.parameters.get(type);
			if(!value.startsWith("/")){
				value = "/"+value;
			}
			request.getRequestDispatcher(value).forward(request, response);
		} else {
			response.sendRedirect(request.getContextPath()+this.parameters.get(ERROR_PAGE));
		}
	}
	
}
